﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using WbfsSync.Core;
using WBFSe3.IO;

namespace WbfsSync.Controls
{
    //---------------------------------------------------------------------------------------------------
    //
    //---------------------------------------------------------------------------------------------------
    public delegate void PathChangedEventHandler(String path);


    //---------------------------------------------------------------------------------------------------
    // Interaction logic for WSPathBox.xaml
    //---------------------------------------------------------------------------------------------------
    public partial class PathBox : UserControl
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        int levels = 0;
        string path = String.Empty;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        protected SolidColorBrush inactiveBack;
        protected SolidColorBrush activeBack;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public event PathChangedEventHandler PathChanged;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public String Path { get { return this.path; } }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public PathBox()
        {
            this.inactiveBack = new SolidColorBrush(Color.FromArgb(0xC0, 0xF0, 0xF0, 0xF0));
            this.activeBack = new SolidColorBrush(Color.FromArgb(0xE6, 0xF8, 0xF8, 0xF8));
            
            InitializeComponent();

            this.RectangleBack1.Fill = this.inactiveBack;
            this.MouseEnter += new MouseEventHandler(WSPathBox_MouseEnter);
            this.MouseLeave += new MouseEventHandler(WSPathBox_MouseLeave);

            this.refreshButton.Click += new RoutedEventHandler(refreshButton_Click);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void WSPathBox_MouseLeave(object sender, MouseEventArgs e)
        {
            this.RectangleBack1.Fill = this.inactiveBack;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void WSPathBox_MouseEnter(object sender, MouseEventArgs e)
        {
            this.RectangleBack1.Fill = this.activeBack;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.PathChanged != null)
                this.PathChanged(this.path);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void p_Click(object sender, RoutedEventArgs e)
        {
            int i = this.pathStack.Children.IndexOf(sender as UIElement);
            int delta = this.levels - i - 1;

            this.pathStack.Children.RemoveRange(i + 1, delta);
            this.levels = i + 1;

            this.mainScroll.ScrollToRightEnd();

            int p = this.path.Length;
            for (int j = 0; j <= delta; j++)
                p = this.path.LastIndexOf('\\', p - 1);

            this.path = this.path.Substring(0, p + 1);
            this.iconImage.Source = Extensions.
                GetFolderMiniature(this.path);

            if (this.PathChanged != null)
                this.PathChanged(this.path);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public void SetPath(String path)
        {
            this.path = path;
            if (!this.path.EndsWith("\\"))
                this.path += '\\';

            String[] tokens = path.Split(new char[] { '\\' }, 
                StringSplitOptions.RemoveEmptyEntries);

            this.levels = tokens.Length;
            this.iconImage.Source = Extensions.
                GetFolderMiniature(this.path);

            this.pathStack.Children.Clear();
            foreach (String token in tokens)
            {
                PathButton p = new PathButton();
                p.Click += new RoutedEventHandler(p_Click);
                p.Margin = new Thickness(0, 2, 0, 2);


                if (token.StartsWith("%"))
                {
                    p.Text = EnvVars.GetVarString(token);
                }
                else if (token.EndsWith(":"))
                {
                    WbfsDriveInfo d;
                    WbfsDriveInfo.GetDrive(token, out d);
                    p.Text = Core.Language.GetDriveString(d);
                }
                else
                {
                    p.Text = token;
                }
                
                this.pathStack.Children.Add(p);
            }

            this.mainScroll.ScrollToRightEnd();
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public void AppendPath(String path)
        {
            String[] tokens = path.Split(new char[] { '\\' },
                StringSplitOptions.RemoveEmptyEntries);

            this.path += path;
            if (!this.path.EndsWith("\\"))
                this.path += '\\';

            this.levels += tokens.Length;
            this.iconImage.Source = Extensions.
                GetFolderMiniature(this.path);

            foreach (String token in tokens)
            {
                PathButton p = new PathButton();
                p.Click += new RoutedEventHandler(p_Click);
                p.Margin = new Thickness(0, 2, 0, 2);
                p.Text = token;

                this.pathStack.Children.Add(p);
            }

            this.mainScroll.ScrollToRightEnd();
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public delegate void SetProgressDelegate(double maximum, double value);
        public void BeginSetProgress(double maximum, double value)
        {
            this.Dispatcher.BeginInvoke((SetProgressDelegate)delegate
                (double max, double val)
                {
                    if (max == 0)
                    {
                        this.MainProgress.Visibility = Visibility.Hidden;
                    }
                    else
                    {
                        this.MainProgress.Visibility = Visibility.Visible;
                        this.MainProgress.Maximum = max;
                        this.MainProgress.Value = val;
                    }
                }, maximum, value);
        }
    }
}
